package com.ly.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ly.entity.Budget;
import com.ly.pojo.dto.BudgetDTO;
import com.ly.pojo.vo.BudgetVO;
import com.ly.service.BudgetService;
import com.ly.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author : 黑渊白花
 * @ClassName BudgetController
 * @date : 2025/2/10 21:48
 * @Description
 */
@CrossOrigin //解决跨域问题
@RestController
@RequestMapping("/budget")
@Slf4j
public class BudgetController {
    @Autowired
    private BudgetService budgetService;
    @GetMapping("/list")
    public Result list(){
        List<Budget> list = budgetService.list();
        return Result.success(list);
    }

    @GetMapping("/Allpage")
    public Object Allpage( BudgetDTO budgetDTO){
        log.info("{}",budgetDTO);
        IPage<BudgetVO> page=null;
        page= budgetService.getAllPage(budgetDTO);
        return Result.success(page);
    }

    @GetMapping("/page")
    public Object page( BudgetDTO budgetDTO){
        log.info("{}",budgetDTO);
        IPage<BudgetVO> page=null;
        page= budgetService.getPage(budgetDTO);
        return Result.success(page);
    }

    @PutMapping("/update")
    public Result update(@RequestBody Budget budget){
        budgetService.updateById(budget);
        return Result.success();
    }

    @PostMapping("/save")
    public Result save(@RequestBody BudgetVO budgetVO){
        Boolean b = budgetService.saveBudget(budgetVO);
        if(b==false){
            return Result.warn("添加失败，数据缺失");
        }
        return Result.success("添加成功");
    }


    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        budgetService.removeById(id);
        return Result.success("删除成功");
    }

}
